#
# @lc app=leetcode.cn id=213 lang=python
#
# [213] 打家劫舍 II
#

# @lc code=start
class Solution(object):
    def rob(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if len(nums) == 1:
            return nums[0]
        if len(nums) == 2:
            return max(nums[0], nums[1])
        if len(nums) == 3:
            return max(nums[0], nums[1], nums[2])
        
        result1 = self.robcode(nums[:-1]) # 打劫第一个
        result2 = self.robcode(nums[1:]) # 不打劫第一个
        return max(result1, result2)

    def robcode(self, nums):
        dp = [0]*3
        dp[0] = nums[0]
        dp[1] = nums[1]
        dp[2] = nums[0]+nums[2]
        for i in range(3, len(nums)):
            temp = max(dp[0], dp[1])+nums[i]
            dp[0] = dp[1]
            dp[1] = dp[2]
            dp[2] = temp
        return max(dp[1], dp[2])
        
# @lc code=end

